//
//  HomePicDetailsDSVC.m
//  ioschishan
//
//  Created by 马田成 on 2017/8/10.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomePicDetailsDSVC.h"
#import "HomePicDetailsDSCell.h"
#import "HomePicDetailsModel.h"
#import "MJRefresh.h"

@interface HomePicDetailsDSVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;
@end

@implementation HomePicDetailsDSVC
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self creationData];
    __weak typeof(self) weakself = self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself creationDataJZ];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself creationData];
        
        [self.tableView.mj_header beginRefreshing];
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArr = [NSMutableArray array];
    [self creationView];
    [self regeditCell];
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    [self setTitle:@"收到的打赏"];
    
    UIView *viewOf = [[UIView alloc] init];
    viewOf.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1);
    [self.view addSubview:viewOf];
    viewOf.backgroundColor = [UIColor colorWithRed:204 / 255.0f green:204 / 255.0f  blue:204 / 255.0f  alpha:1];
    
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;
    //   self.tableView.separatorStyle = UITableViewCellAccessoryNone;去掉分割线
    
    
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor colorWithRed:245 / 255.0f green:245 / 255.0f blue:245 / 255.0f alpha:1];
    
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HomePicDetailsDSCell" bundle:nil] forCellReuseIdentifier:@"HomePicDetailsDSCell"];
    
}
//- (NSMutableArray *)dataArr{
//
//    if (_dataArr) {
//        _dataArr = [NSMutableArray array];
//    }
//    return _dataArr;
//    
//}
- (void)creationData{
    
    
    
    [self.tableView.mj_header endRefreshing];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    _page = 1;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:strPage forKey:@"page"];
    [productParams setValue:_circle_id forKey:@"circle_id"];
    [_dataArr removeAllObjects];
    
    [[MNetworkHandler sharedInstance]conURL:home_CirclerewardRecord networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {

            NSMutableArray *arrOfTie = [returnData objectForKey:@"data"];
            
            for (NSDictionary *dic in arrOfTie ) {
                HomePicDetailsModel *model = [[HomePicDetailsModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                [_dataArr addObject:model];
                NSLog(@"%@", _dataArr);
            }
            
            [self.tableView reloadData];
            
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];
    }];
    
}

- (void)creationDataJZ{
    
    _page++;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //  NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:strPage forKey:@"page"];
    [productParams setValue:_circle_id forKey:@"circle_id"];
    [[MNetworkHandler sharedInstance]conURL:home_CirclerewardRecord networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            NSMutableArray *arr = [returnData objectForKey:@"data"];
            [self.dataArr addObjectsFromArray:arr];
            
        
            
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
}



#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 70;
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomePicDetailsDSCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomePicDetailsDSCell" forIndexPath:indexPath];
    HomePicDetailsModel *model = self.dataArr[indexPath.row];
    cell.model = model;
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
